summaryrefslogtreecommitdiff
path: root/app/api/partners/tbe/[sessionId]/documents/route.ts
blob: 0045ea43608fb1bf2276744add47acdab669275e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// app/api/partners/tbe/[sessionId]/documents/route.ts
import { NextRequest, NextResponse } from "next/server"
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/app/api/auth/[...nextauth]/route"
import db from "@/db/db"
import { 
  rfqLastTbeDocumentReviews,
  rfqLastTbeSessions,
  rfqLastTbeHistory,
  rfqLastTbeVendorDocuments
} from "@/db/schema"
import { eq, and } from "drizzle-orm"
import { writeFile, mkdir } from "fs/promises"
import { createWriteStream } from "fs"
import { pipeline } from "stream/promises"
import path from "path"
import { v4 as uuidv4 } from "uuid"

// 1GB 파일 지원을 위한 설정
export const config = {
  api: {
    bodyParser: {
      sizeLimit: '1gb',
    },
    responseLimit: false,
  },
}

// 스트리밍으로 파일 저장
async function saveFileStream(file: File, filepath: string) {
  const stream = file.stream()
  const writeStream = createWriteStream(filepath)
  await pipeline(stream, writeStream)
}

// POST: TBE 문서 업로드
export async function POST(request: NextRequest, { params }: { params: { sessionId: string } }) {
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user || session.user.domain !== "partners") {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
    }

    const tbeSessionId = Number(params.sessionId)
    const formData = await request.formData()

    // ✅ 프런트 기frfqLastTbeVendorDocuments본값 'other' 등을 안전한 enum으로 매핑
    const documentType = (formData.get("documentType") as string | undefined)
    const documentName = (formData.get("documentName") as string | undefined)?.trim() || "Untitled"
    const description = (formData.get("description") as string | undefined) || ""
    const file = formData.get("file") as File | null

    if (!file) {
      return NextResponse.json({ error: "파일이 필요합니다" }, { status: 400 })
    }

    // 세션/권한
    const tbeSession = await db.query.rfqLastTbeSessions.findFirst({
      where: eq(rfqLastTbeSessions.id, tbeSessionId),
      with: { vendor: true },
    })
    if (!tbeSession) return NextResponse.json({ error: "TBE 세션을 찾을 수 없습니다" }, { status: 404 })

    // 권한 체크: 회사 기준으로 통일 (위/아래 GET도 동일 기준을 권장)
    if (tbeSession.vendor?.id !== session.user.companyId) {
      return NextResponse.json({ error: "권한이 없습니다" }, { status: 403 })
    }

    // 저장 경로
    const isDev = process.env.NODE_ENV === "development"
    const uploadDir = isDev
      ? path.join(process.cwd(), "public", "uploads", "tbe", String(tbeSessionId), "vendor")
      : path.join(process.env.NAS_PATH || "/nas", "uploads", "tbe", String(tbeSessionId), "vendor")

    await mkdir(uploadDir, { recursive: true })

    const safeOriginal = file.name.replace(/[^a-zA-Z0-9.\-_\s]/g, "_")
    const filename = `${uuidv4()}_${safeOriginal}`
    const filepath = path.join(uploadDir, filename)

    try {
      if (file.size > 50 * 1024 * 1024) {
        await saveFileStream(file, filepath)
      } else {
        const buffer = Buffer.from(await file.arrayBuffer())
        await writeFile(filepath, buffer)
      }
    } catch (e) {
      console.error("파일 저장 실패:", e)
      return NextResponse.json({ error: "파일 저장에 실패했습니다" }, { status: 500 })
    }

    // 트랜잭션
    const result = await db.transaction(async (tx) => {
      // 1) 벤더 업로드 문서 insert
      const [vendorDoc] = await tx
        .insert(rfqLastTbeVendorDocuments)
        .values({
          tbeSessionId,
          documentType, // enum 매핑된 값
          isResponseToReviewId: null, // 필요 시 formData에서 받아 세팅
          fileName: filename,
          originalFileName: file.name,
          filePath: `/uploads/tbe/${tbeSessionId}/vendor/${filename}`,
          fileSize: Number(file.size),
          fileType: file.type || null,
          documentNo: null,
          revisionNo: null,
          issueDate: null,
          description,
          submittalRemarks: null,
          reviewRequired: true,
          reviewStatus: "pending",
          submittedBy: session.user.id,
          submittedAt: new Date(),
          reviewedBy: null,
          reviewedAt: null,
          reviewComments: null,
        })
        .returning()

      // 2) (선택) 기존 리뷰 테이블에도 “벤더가 올린 검토대상 문서”로 남기고 싶다면 유지
      //    필요 없다면 아래 블록은 제거 가능
      const [documentReview] = await tx
        .insert(rfqLastTbeDocumentReviews)
        .values({
          tbeSessionId,
          vendorAttachmentId:vendorDoc.id,
          documentSource: "vendor",
          documentType: documentType, // 동일 매핑
          documentName: documentName, // UX 표시용 이름
          reviewStatus: "미검토",
          reviewComments: description,
          createdAt: new Date(),
          updatedAt: new Date(),
        })
        .returning()

      // 3) 세션 상태 전환
      if (tbeSession.status === "준비중") {
        await tx
          .update(rfqLastTbeSessions)
          .set({
            status: "진행중",
            actualStartDate: new Date(),
            updatedAt: new Date(),
            updatedBy: session.user.id,
          })
          .where(eq(rfqLastTbeSessions.id, tbeSessionId))
      }

      // 4) 이력
      await tx.insert(rfqLastTbeHistory).values({
        tbeSessionId,
        actionType: "document_review",
        changeDescription: `벤더 문서 업로드: ${documentName}`,
        changeDetails: {
          vendorDocumentId: vendorDoc.id,
          documentReviewId: documentReview.id,
          documentName: documentName,
          documentType: documentType,
          filePath: vendorDoc.filePath,
        },
        performedBy: session.user.id,
        performedByType: "vendor",
        performedAt: new Date(),
      })

      if (tbeSession.status === "준비중") {
        await tx.insert(rfqLastTbeHistory).values({
          tbeSessionId,
          actionType: "status_change",
          previousStatus: "준비중",
          newStatus: "진행중",
          changeDescription: "벤더 문서 업로드로 인한 상태 변경",
          performedBy: session.user.id,
          performedByType: "vendor",
          performedAt: new Date(),
        })
      }

      return {
        vendorDoc,
        documentReview,
      }
    })

    return NextResponse.json({
      success: true,
      data: {
        vendorDocumentId: result.vendorDoc.id,
        filePath: result.vendorDoc.filePath,
        originalFileName: result.vendorDoc.originalFileName,
        fileSize: result.vendorDoc.fileSize,
        fileType: result.vendorDoc.fileType,
      },
      message: "문서가 성공적으로 업로드되었습니다",
    })
  } catch (error) {
    console.error("TBE 문서 업로드 오류:", error)
    return NextResponse.json({ error: "문서 업로드에 실패했습니다" }, { status: 500 })
  }
}

// GET: TBE 세션의 문서 목록 조회
export async function GET(
  request: NextRequest,
  { params }: { params: { sessionId: string } }
) {
  try {
    const session = await getServerSession(authOptions)
    if (!session?.user || session.user.domain !== "partners") {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
    }
    
    const tbeSessionId = parseInt(params.sessionId)
    
    // TBE 세션 확인 및 권한 체크
    const tbeSession = await db.query.rfqLastTbeSessions.findFirst({
      where: eq(rfqLastTbeSessions.id, tbeSessionId),
      with: {
        vendor: true,
        documentReviews: {
          orderBy: (reviews, { desc }) => [desc(reviews.createdAt)],
        }
      }
    })
    
    if (!tbeSession) {
      return NextResponse.json({ error: "TBE 세션을 찾을 수 없습니다" }, { status: 404 })
    }
    
    // 벤더 권한 확인
    if (tbeSession.vendor.userId !== session.user.id) {
      return NextResponse.json({ error: "권한이 없습니다" }, { status: 403 })
    }
    
    // PDFTron 코멘트 수 집계 (필요시)
    const documentsWithDetails = await Promise.all(
      tbeSession.documentReviews.map(async (doc) => {
        // PDFTron 코멘트 수 조회
        const pdftronComments = await db.query.rfqLastTbePdftronComments.findFirst({
          where: eq(rfqLastTbePdftronComments.documentReviewId, doc.id),
        })
        
        return {
          ...doc,
          comments: pdftronComments?.commentSummary || {
            totalCount: 0,
            openCount: 0,
          },
        }
      })
    )
    
    return NextResponse.json({
      success: true,
      session: {
        id: tbeSession.id,
        sessionCode: tbeSession.sessionCode,
        sessionTitle: tbeSession.sessionTitle,
        sessionStatus: tbeSession.status,
        evaluationResult: tbeSession.evaluationResult,
      },
      documents: documentsWithDetails,
    })
    
  } catch (error) {
    console.error("문서 목록 조회 오류:", error)
    return NextResponse.json(
      { error: "문서 목록 조회에 실패했습니다" },
      { status: 500 }
    )
  }
}